package com.ruoyi.common.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.profile.Language;
import com.tencentcloudapi.cvm.v20170312.CvmClient;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;
import com.tencentcloudapi.cvm.v20170312.models.Filter;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.TextWaybill;
import com.tencentcloudapi.ocr.v20181119.models.WaybillOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.WaybillOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.WaybillObj;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tanjianjun
 * @version 1.o
 * @description WaybillOCR
 * @since 2021/4/14 21:20
 */
public class WaybillOCR {
    static Credential cred = new Credential("AKIDLVQ8akyLjsnaCc6V7PRFnnY3HI4WLjNn", "RfssjMioiUyCo7B2aosjHJ4GD0GCfqVv");
    static HttpProfile httpProfile = new HttpProfile();
    static ClientProfile clientProfile = new ClientProfile();
    static WaybillOCRRequest req = new WaybillOCRRequest();

    public static Map<String,String> Get(String url) {
        Map<String, String> map = new HashMap<>();
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密

            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);

            req.setImageUrl(url);
            WaybillOCRResponse resp = client.WaybillOCR(req);
            TextWaybill textDetections = resp.getTextDetections();
            String waybillNum = textDetections.getWaybillNum().getText();
            String senderName = textDetections.getSenderName().getText();
            String senderNum = textDetections.getSenderNum().getText();
            String senderAddr = textDetections.getSenderAddr().getText();
            String recName = textDetections.getRecName().getText();
            String recAddr = textDetections.getRecAddr().getText();
            String recNum = resp.getTextDetections().getRecNum().getText();

            map.put("waybillNum",waybillNum);
            map.put("recNum",recNum);
            map.put("recName",recName);
            map.put("recAddr",recAddr);

            map.put("senderName",senderName);
            map.put("senderNum",senderNum);
            map.put("senderAddr",senderAddr);


            System.out.println(WaybillOCRResponse.toJsonString(resp));
            return map;
        } catch (TencentCloudSDKException e) {
            map.put("waybillNum",null);
            map.put("recNum",null);
            System.out.println(e.toString());
            return map;
        }
    }
}
